package com.jpyl.jpMedicalTreatment.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ProductsMessageModel;

import java.util.List;

/**
 * 我的优惠适配器
 * Created by 15697 on 2019/1/5.
 */

public class MyDiscountAdapter extends RecyclerView.Adapter<MyDiscountAdapter.ViewHolder>
        implements View
        .OnClickListener {
    private List<ProductsMessageModel.ProductsModel> list;
    private int flag;

    public MyDiscountAdapter(List<ProductsMessageModel.ProductsModel> list, int flag) {
        this.list = list;
        this.flag = flag;
    }

    @Override
    public MyDiscountAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                        .activity_item_my_discount,
                null);
        view.setOnClickListener(this);
        return new MyDiscountAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyDiscountAdapter.ViewHolder holder, final int position) {
        holder.tv_title.setText(list.get(position).getName());
        holder.tv_price.setText("原价格：" + list.get(position).getPrice() + "元");
        switch (flag) {
            case 1:
                holder.tv_msg.setText("现价格：" + list.get(position).getSaleprice() + "元");
                break;
            case 2:
                holder.tv_msg.setText("兑换需要积分：" + list.get(position).getPointsneed() + "分");
                break;
        }
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View view) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_price, tv_msg;
        ImageView img_pic;

        ViewHolder(View convertView) {
            super(convertView);
            tv_title = convertView.findViewById(R.id.tv_itemMyDiscount_title);
            tv_price = convertView.findViewById(R.id.tv_itemMyDiscount_price);
            tv_msg = convertView.findViewById(R.id.tv_itemMyDiscount_msg);
            img_pic = convertView.findViewById(R.id.img_itemMyDiscount_pic);


        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private MyDiscountAdapter.OnItemClickListener mItemClickListener;

    public void setItemClickListener(MyDiscountAdapter.OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

}
